package fi.jamk.ourcookingapp.backend.architecture.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import fi.jamk.ourcookingapp.backend.MyTools;
import fi.jamk.ourcookingapp.backend.architecture.interfaces.IUserManager;
import fi.jamk.ourcookingapp.backend.architecture.json.WebService;
import fi.jamk.ourcookingapp.backend.exceptions.NoUserException;
import fi.jamk.ourcookingapp.backend.exceptions.WebServiceException;
import fi.jamk.ourcookingapp.backend.model.User;

public class UserManager implements IUserManager {
	
	private static final String CONTROLLER = "user";
	
	@Override
	public boolean addToFriendList(User user, User userToAdd) throws InterruptedException, ExecutionException, JSONException {
		String getData = "user_id=>" + user.getID() + "&friend_id=>" + userToAdd.getID();
		AsyncTask<String, Void, JSONObject> result = new WebService().execute(getData, CONTROLLER, "addToFriendList");
		boolean obj = result.get().getBoolean("user");
		return obj;
	}
	
	@Override
	public User login(User user) throws InterruptedException, ExecutionException, JSONException, WebServiceException{
		String getData = "loginName=>" + user.getLoginName() + "&password=>" + user.getPassword();
		AsyncTask<String, Void, JSONObject> result = new WebService().execute(getData, CONTROLLER, "login");
		JSONObject obj = result.get();
		JSONObject jsonUser = obj.getJSONObject("user");
		if(jsonUser.has("error")){
			throw new WebServiceException(jsonUser.getString("error"));
		}
		return new User(jsonUser.getString("loginName"), jsonUser.getInt("id"));
	}

	@Override
	public User createUser(User user) throws InterruptedException, ExecutionException, WebServiceException, JSONException {
		String getData = "loginName=>" + user.getLoginName() + "&password=>" + user.getPassword() + "&email=>" + user.getEMail();
		AsyncTask<String, Void, JSONObject> result = new WebService().execute(getData, CONTROLLER, "create");
		try {
			JSONObject JUser = result.get().getJSONObject("user");
			String error = JUser.getString("error");
			if(!MyTools.isEmpty(error)){
				throw new WebServiceException(error);
			}
		} catch (JSONException e) {
			user.setID(result.get().getInt("user"));
		}
		return user;
	}

	@Override
	public List<User> friendList(User user) throws InterruptedException, ExecutionException, NoUserException {
		List<User> result = new ArrayList<User>();
		String getData = "user_id=>" + user.getID();
		AsyncTask<String, Void, JSONObject> JSONResult = new WebService().execute(getData, CONTROLLER, "friendList");
		JSONObject obj = JSONResult.get();
		try {
			JSONArray jsArray = obj.getJSONArray("friendList");
			for (int i=0;i < obj.length();i++) {
				JSONObject JSONuser = jsArray.getJSONObject(i);
				User tmp = new User(JSONuser.getString("loginName"), JSONuser.getInt("id"));
				result.add(tmp);
			}
		} catch (JSONException e) {
			JSONObject JSONuser;
			try {
				JSONuser = obj.getJSONObject("friendList");
				User tmp = new User(JSONuser.getString("loginName"), JSONuser.getInt("id"));
				result.add(tmp);
			} catch (JSONException e1) {
				throw new NoUserException();
			}
		}
		return result;
	}
	
}
